﻿using Advertising.Business.Common.Context;
using Advertising.Business.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Advertising.Business.Server
{
    public class CategoryService : ICategoryService
    {
        private readonly CategoryContext context;

        public CategoryService()
        {
            context = new CategoryContext();
        }

        public int AddNewCategory(Category category)
        {
            IDataParameter ReturnId = context.Category.BuildParameter("ReturnId", DbType.Int32, ParameterDirection.Output);
            context.Category.Execute("Category_Add", context.Category.BuildParameter("Name", DbType.String, category.Name),
                                            context.Category.BuildParameter("Description", DbType.String, category.Description),
                                            context.Category.BuildParameter("FileName", DbType.String, category.IconPath),
                                            ReturnId);
            return (Int32)ReturnId.Value;
        }


        public ICollection<Category> ListCategories()
        {
            var categories = context.Category.Init("Category_List")
                .MapCollection<Category>()
                .GetCollection();

            return categories;
        }


        public int EditCategory(Category category)
        {
            IDataParameter rowEffect = context.Category.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Category.Execute("Category_Edit", context.Category.BuildParameter("CatId", DbType.String, category.CatId),
                context.Category.BuildParameter("Name", DbType.String, category.Name),
                                            context.Category.BuildParameter("Description", DbType.String, category.Description),
                                            rowEffect);
            return (Int32)rowEffect.Value;
        }


        public int DeleteCategory(int id)
        {
            IDataParameter rowEffect = context.Category.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Category.Execute("Category_Delete", context.Category.BuildParameter("CatId", DbType.String, id),
                                            rowEffect);
            return (Int32)rowEffect.Value;
        }
    }
}
